const fs = require('fs')
const join = require('path').join;
const { exit } = require('process');

const argv = process.argv
if (argv.length <= 2) {
    console.log('usage: node htmlSimpleJKmd.js srcPath dstPath')
    exit
}
let srcPath = argv[2] !== undefined ? argv[2] : "md"
let dstPath = argv[3] !== undefined ? argv[3] : "md_simple"
console.log(srcPath, dstPath);

// let srcPath = "html";
// let dstPath = "md";
try{
  fs.mkdirSync(dstPath);
}catch (error){
}

let files = fs.readdirSync(srcPath);
files.forEach(function (item, index) {
    let fPath = join(srcPath,item);
    console.log(fPath, item, index);

    let stat = fs.statSync(fPath);
    if (stat.isFile() === true &&  -1 !== item.indexOf(".md")) { 
      let md = simpleHtml(fPath);
      let dstFile = join(dstPath, item).replace(".md", ".md");
      fs.writeFileSync(dstFile, md, 'utf8' );
    }
});

function simpleHtml(file){
  let content = fs.readFileSync(file, 'utf8' );
  content = content.replace(/(.|\n)*(推荐作者|文章详情)/g, "");
  // content = content.replace(/复制代码/g, "");
  return content;
}









